package edu.snake.implement;


import edu.snake.core.GameEngine;
import edu.snake.core.model.character.CharactersBuilder;
import edu.snake.core.service.LiveOrDieService;
import edu.snake.core.service.ThreadPoolService;
import edu.snake.core.service.ViewController;
import edu.snake.core.view.ViewFactory;
import edu.snake.implement.model.CharacterName;
import edu.snake.implement.model.character.listenerDecorator.ListenersImpl;
import edu.snake.implement.service.LiveOrDieServiceImpl;
import edu.snake.implement.service.ThreadPoolServiceImpl;
import edu.snake.implement.view.ViewRenderFactorySwing;
import edu.snake.implement.view.controller.ViewControllerImpl;

public class ConfigConstant {
    public static int cellWidth = 0;
    public static int cellHeight = 0;
    public static int snakeSize = 0;
    public static int frogCount = 0;
    public static int millisSnake = 0;
    public static int multiplierDelay = 0;
    public static int cellSizePixel = 0;
    private static volatile ViewFactory viewFactory;
    private static volatile ViewController viewController;
    private static volatile GameEngine gameEngine;
    private static volatile CharactersFactory charactersFactory;
    private static volatile ThreadPoolService threadPoolService;
    private static volatile LiveOrDieService liveOrDieService;

    public static GameEngine getInstanceGameEngine() {

        if (gameEngine == null) {
            synchronized (ConfigConstant.class) {
                if (gameEngine == null) {
                    gameEngine = new GameEngineImpl();
                }
            }
        }
        return gameEngine;
    }

    public static ViewController createAndReturnViewController(GameEngine gameEngine) {
        if (gameEngine == null) {
            throw new NullPointerException("Can not create viewController. GameEngine is null");
        }
        if (viewController == null) {
            synchronized (ConfigConstant.class) {
                if (viewController == null) {
                    viewController = new ViewControllerImpl(new ListenersImpl(), gameEngine);
                }
            }
        }
        return viewController;
    }

    public static ViewController getInstanceViewController() {
        if (viewController == null) {
            throw new NullPointerException("CCan not provide a ViewController instance has not been created");
        }
        return viewController;
    }


    public static ViewFactory getInstanceView() {
        if (viewFactory == null) {
            synchronized (ConfigConstant.class) {
                if (viewFactory == null) {
                    viewFactory = new ViewRenderFactorySwing();
                }
            }
        }
        return viewFactory;
    }

    public static CharactersBuilder<CharacterName> getInstanceFactory() {
        if (charactersFactory == null) {
            synchronized (ConfigConstant.class) {
                if (charactersFactory == null) {
                    charactersFactory = new CharactersFactory();
                }
            }
        }
        return charactersFactory;
    }

    public static ThreadPoolService getInstanceThreadPoolService() {
        if (threadPoolService == null) {
            synchronized (ConfigConstant.class) {
                if (threadPoolService == null) {
                    threadPoolService = new ThreadPoolServiceImpl();
                }
            }
        }
        return threadPoolService;
    }

    public static LiveOrDieService getInstanceLiveOrDieService() {
        if (liveOrDieService == null) {
            synchronized (ConfigConstant.class) {
                if (liveOrDieService == null) {
                    liveOrDieService = new LiveOrDieServiceImpl();
                }
            }
        }
        return liveOrDieService;
    }

}
